import { createRouter, createWebHistory } from "vue-router";

import { defineAsyncComponent } from "vue";
const layout = () => import("@/layout/index.vue");
//公共路由
export const constantRoutes = [
  {
    path: "/",
    redirect: "/index",
    component:() => import("../layout/index.vue"),
    children:[
      {
        path: "/index",
        name:"index",
        component:() => import("@/views/index.vue"),
        meta:{
          title:"主页",
          keepAlive:true,
        },
      },

      {
        path: "/messageType",
        name:"messageType",
        component:() => import("@/views/messageType.vue"),
        meta:{
          title:"消息分类"
        },
      },
      

      {
        path: "/personal",
        name:"personal",
        component:() => import("@/views/personal.vue"),
        meta:{
          // transition:"slide-fade",
          title:'个人中心'
        }
      },{
        path: "/list/:message",
        name:"list",
        beforeEnter: (to, from, next) => {
          if (to.params.message === 'message') {
            to.meta.title = '公文消息'
          } else {
            to.meta.title = '待签发公文'
          }
          next()
        },
        component:() => import("@/views/list.vue"),
        meta:{
          transition:"slide-fade",
          keepAlive:true,
        }
        
      },{
        path: "/detail/:type",
        name:"detail",
        beforeEnter: (to, from, next) => {
          if (to.params.type === 'message') {
            to.meta.title = '消息详情'
          } else {
            to.meta.title = '消息签发'
          }
          next()
        },
        component:() => import("@/views/detail.vue"),
        meta:{
          transition:"slide-fade",
        }
      }
    ]
  },
];

const router = createRouter({
  routes: constantRoutes,
  history: createWebHistory("/"), //history模式使用HTML5模式
});

router.beforeResolve((to, from, next) => {
  window.document.title = to.meta.title
  next()
})



export const routerPath = (name,isParams = false) => {
  if(!isParams){
    router.push(name)
  }else{
    router.push({
      name,
      params: isParams
    })
  }
  
}

export default router;
